package com.example.d10_homework.homework08;

import java.util.Random;

public class MyRunnable implements Runnable {
    private int a = 10;
    private Random r = new Random();

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        while (true) {
            // 生成1-100之间的随机数
            try {
                synchronized (this) {
                    int num = r.nextInt(100) + 1;
                    if (num != a) {
                        System.out.println(name + "猜的数字为" + num);
                        Thread.sleep(1000);
                        this.notifyAll();
                        this.wait();
                    } else {
                        System.out.println(name + "猜对了");
                        this.wait();
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
